<template>
	<view class="wrap">
		<view v-if="isInit" v-cloak>
			<view class="btns">
				<button class="loginBtn" @click="sync">同步</button>
				<button class="loginBtn" @click="refresh">刷新</button>
			</view>
			<noRecord v-if="menuInfo.list.length == 0" :info="info" v-cloak></noRecord>
			<menuList v-else :menuInfo="menuInfo" @loadMore="addPage" :screenH = "screenH"></menuList>
		</view>
	</view>
</template>
<script>
import menuList from '@/wxcomponents/commonLayout/menuList.vue';
import noRecord from '@/wxcomponents/commonLayout/noRecord.vue';
import { format ,screenHeight} from '@/libs/common.js';
import dictMixins from '@/libs/dictMixins.js';
export default {
	mixins: [dictMixins],
	data() {
		return {
			isInit: false,
			info: '没有记录!',
			menuInfo: {
				base: {
					menuProp: ['teacherName', 'courseName', 'lessonDate', 'teachingStatus'],
					tit: ['姓名', '课程', '日期', '状态'],
					class: 'typeOne',
					loadMore: true
				},
				list: []
			},
			pageParams: {
				pageNum: 1,
				pageSize: 20
			},
			// read 只有已填写数据  all  未填写、已填写都有
			type: '',
			screenH:screenHeight * 0.7
		};
	},
	onShow() {
		let pageOptions = this.getPageOptions();
		this.type = pageOptions.type;
		this.menuInfo.list = [];
		this.getList();
	},
	onHide() {
		this.isInit = false;
	},
	methods: {
		refresh() {
			this.pageParams={
				pageNum: 1,
				pageSize: 20
			},
			this.menuInfo.list = [];
			this.getList();
		},
		async sync() {
			let resp = await this.$http.post({
				url: '/edu/schoolJournal/refresh',
				data: {}
			});
		},
		async getList(e) {
			let self = this;
			let funcMap = [
				{
					type: 'read',
					async func() {
						let resp = await self.$http.post({
							url: '/edu/schoolJournal/page',
							data: self.pageParams
						});
						let list = resp.result.records;
						if (list.length < self.pageParams.pageSize || list.length == 0) {
							self.menuInfo.base.loadMore = false;
						}
						list = list
							.filter(item => {
								return item.lessonCondition;
							})
							.map(item => {
								item.teachingStatus = item.lessonCondition ? '已填写' : '待填写';
								let obj = {
									url: `detail?data=${JSON.stringify(item)}`,
									teacherName: item.teacherName,
									courseName: item.courseName,
									lessonDate: item.lessonDate.substring(5, 10),
									teachingStatus: item.teachingStatus
								};
								return obj;
							});
						self.menuInfo.list = self.menuInfo.list.concat(list);
						self.isInit = true;
					}
				},
				{
					type: 'all',
					url: '/edu/schoolJournal/wechat/page',
					async func() {
						let resp = await self.$http.post({
							url: '/edu/schoolJournal/wechat/page',
							data: self.pageParams
						});
						let list = resp.result.records;
						if (list.length < self.pageParams.pageSize || list.length == 0) {
							self.menuInfo.base.loadMore = false;
						}
						list = list.map(item => {
							item.teachingStatus = item.lessonCondition ? '已填写' : '待填写';
							let obj = {
								url: `detail?data=${JSON.stringify(item)}`,
								teacherName: item.teacherName,
								courseName: item.courseName,
								lessonDate: item.lessonDate.substring(5, 10),
								teachingStatus: item.teachingStatus
							};
							return obj;
						});
						self.menuInfo.list = self.menuInfo.list.concat(list);
						self.isInit = true;
					}
				}
			];
			funcMap
				.filter(item => {
					return item.type == this.type;
				})[0]
				.func();
		},
		// 加载下一页
		addPage() {
			this.pageParams.pageNum++;
			this.getList();
		}
	},
	components: { menuList, noRecord }
};
</script>

<style lang="scss" scoped>
.btns {
	display: flex;
	padding: 20upx 30upx;
}
[v-cloak] {
	display: none !important;
}
</style>
